include:
  - local: .gitlab/ci/global.gitlab-ci.yml
  - local: .gitlab/ci/rules.gitlab-ci.yml

.rules:dont-interrupt:
  rules:
    - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
      allow_failure: true
    - if: $CI_MERGE_REQUEST_IID
      when: manual
      allow_failure: true

#######################
# rspec job base specs
.rails-job-base:
  extends:
    - .default-retry
    - .default-before_script
    - .ruby-cache

.base-script:
  script:
    - source ./scripts/rspec_helpers.sh
    # Only install knapsack after bundle install! Otherwise oddly some native
    # gems could not be found under some circumstance. No idea why, hours wasted.
    - run_timed_command "gem install knapsack --no-document"
    - section_start "gitaly-test-spawn" "Spawning Gitaly"; scripts/gitaly-test-spawn; section_end "gitaly-test-spawn"  # Do not use 'bundle exec' here
    - export RSPEC_SKIPPED_TESTS_REPORT_PATH="rspec/skipped_tests-${CI_JOB_ID}.txt"
    - export RSPEC_RETRIED_TESTS_REPORT_PATH="rspec/retried_tests-${CI_JOB_ID}.txt"

.no-redis-cluster:
  variables:
    USE_REDIS_CLUSTER: "false"

.clusterwide-db:
  variables:
    CLUSTERWIDE_DB: "true"

.single-db:
  variables:
    DECOMPOSED_DB: "false"

.single-db-ci-connection:
  extends: .single-db
  variables:
    CI_CONNECTION_DB: "true"

.single-db-rspec:
  extends: .single-db

.single-db-ci-connection-rspec:
  extends: .single-db-ci-connection

.praefect-with-db:
  variables:
    GITALY_PRAEFECT_WITH_DB: '1'

.rspec-base:
  extends:
    - .rails-job-base
    - .base-artifacts
  stage: test
  variables:
    RUBY_GC_MALLOC_LIMIT: 67108864
    RUBY_GC_MALLOC_LIMIT_MAX: 134217728
    RECORD_DEPRECATIONS: "true"
    GEO_SECONDARY_PROXY: 0
    SUCCESSFULLY_RETRIED_TEST_EXIT_CODE: 137
  needs:
    - job: "setup-test-env"
    - job: "retrieve-tests-metadata"
    - job: "compile-test-assets"
    - job: "detect-tests"
      optional: true
  script:
    - !reference [.base-script, script]
    # We need to exclude background migration because unit tests run with
    # spec/lib, yet background migration tests are also sitting there,
    # and they should run on their own jobs so we don't need to run them
    # in unit tests again.
    - rspec_paralellized_job "--fail-fast=${RSPEC_FAIL_FAST_THRESHOLD} --tag ~quarantine --tag ~level:background_migration --tag ~click_house"
  after_script:
    - echo -e "\e[0Ksection_start:`date +%s`:report_results_section[collapsed=true]\r\e[0KReport results"
    - |
      if [ "$CREATE_RAILS_TEST_FAILURE_ISSUES" == "true" ]; then
        bundle exec relate-failure-issue --input-files "rspec/rspec-*.json" --system-log-files "log" --project "gitlab-org/gitlab" --token "${TEST_FAILURES_PROJECT_TOKEN}";
      fi
      if [ "$CREATE_RAILS_SLOW_TEST_ISSUES" == "true" ]; then
        bundle exec slow-test-issues --input-files "rspec/rspec-*.json" --project "gitlab-org/gitlab" --token "${TEST_FAILURES_PROJECT_TOKEN}";
      fi
    - echo -e "\e[0Ksection_end:`date +%s`:report_results_section\r\e[0K"

  allow_failure:
    exit_codes: !reference [.rspec-base, variables, SUCCESSFULLY_RETRIED_TEST_EXIT_CODE]

.base-artifacts:
  artifacts:
    expire_in: 31d
    when: always
    paths:
      - auto_explain/
      - coverage/
      - crystalball/
      - deprecations/
      - knapsack/
      - rspec/
      - tmp/capybara/
      - log/*.log
    reports:
      junit: "rspec/rspec-*.xml"

.rspec-base-migration:
  script:
    - !reference [.base-script, script]
    - rspec_paralellized_job "--fail-fast=${RSPEC_FAIL_FAST_THRESHOLD} --tag ~quarantine --tag ~zoekt --tag ~click_house"
  after_script:
    - !reference [.rspec-base, after_script]

.rspec-base-pg12:
  extends:
    - .rspec-base
    - .use-pg12

.rspec-base-pg13:
  extends:
    - .rspec-base
    - .use-pg13

.rspec-base-pg14:
  extends:
    - .rspec-base
    - .use-pg14

.rspec-base-pg14-clickhouse23:
  extends:
    - .rspec-base
    - .use-pg14-clickhouse23
  script:
    - cp config/click_house.yml.example config/click_house.yml
    - 'sed -i "s|url:.*$|url: http://clickhouse:8123|g" config/click_house.yml'
    - !reference [.base-script, script]
    - rspec_paralellized_job "--fail-fast=${RSPEC_FAIL_FAST_THRESHOLD} --tag click_house"

.rspec-base-pg14-as-if-foss:
  extends:
    - .rspec-base
    - .as-if-foss
    - .use-pg14
  needs:
    - job: "setup-test-env"
    - job: "retrieve-tests-metadata"
    - job: "compile-test-assets as-if-foss"
    - job: "detect-tests"
      optional: true

.rspec-base-pg15:
  extends:
    - .rspec-base
    - .use-pg15

.rspec-ee-base-pg12:
  extends:
    - .rspec-base
    - .use-pg12-es7-ee

.rspec-ee-base-pg13:
  extends:
    - .rspec-base
    - .use-pg13-es7-ee

.rspec-ee-base-pg13-es8:
  extends:
    - .rspec-base
    - .use-pg13-es8-ee
    - .rails:rules:run-search-tests

.rspec-ee-base-pg13-opensearch1:
  extends:
    - .rspec-base
    - .use-pg13-opensearch1-ee
    - .rails:rules:run-search-tests

.rspec-ee-base-pg13-opensearch2:
  extends:
    - .rspec-base
    - .use-pg13-opensearch2-ee
    - .rails:rules:run-search-tests

.rspec-ee-base-pg14:
  extends:
    - .rspec-base
    - .use-pg14-es7-ee

.rspec-ee-base-pg14-es8:
  extends:
    - .rspec-base
    - .use-pg14-es8-ee
    - .rails:rules:run-search-tests

.rspec-ee-base-pg14-opensearch1:
  extends:
    - .rspec-base
    - .use-pg14-opensearch1-ee
    - .rails:rules:run-search-tests

.rspec-ee-base-pg14-opensearch2:
  extends:
    - .rspec-base
    - .use-pg14-opensearch2-ee
    - .rails:rules:run-search-tests

.rspec-ee-base-pg15:
  extends:
    - .rspec-base
    - .use-pg15-es7-ee

.rspec-ee-base-pg15-es8:
  extends:
    - .rspec-base
    - .use-pg15-es8-ee
    - .rails:rules:run-search-tests

.rspec-ee-base-pg15-opensearch1:
  extends:
    - .rspec-base
    - .use-pg15-opensearch1-ee
    - .rails:rules:run-search-tests

.rspec-ee-base-pg15-opensearch2:
  extends:
    - .rspec-base
    - .use-pg15-opensearch2-ee
    - .rails:rules:run-search-tests

.db-job-base:
  extends:
    - .rails-job-base
    - .rails:rules:ee-and-foss-migration
    - .use-pg14
  stage: test
  needs: ["setup-test-env"]
# rspec job base specs
######################

############################
# rspec job parallel configs
# Adjusting these parallel job counts  has an impact on the
# rspec:artifact-collector jobs in .gitlab/ci/rails.gitlab-ci.yml
# Please double-check and adjust accordingly
.rspec-migration-parallel:
  parallel: 8

.rspec-background-migration-parallel:
  parallel: 4

.rspec-ee-migration-parallel:
  parallel: 2

.rspec-ee-background-migration-parallel:
  parallel: 2

.rspec-unit-parallel:
  parallel: 28

.rspec-ee-unit-parallel:
  parallel: 18

.rspec-integration-parallel:
  parallel: 12

.rspec-ee-integration-parallel:
  parallel: 6

.rspec-system-parallel:
  parallel: 28

.rspec-ee-system-parallel:
  parallel: 10
# rspec job parallel configs
############################
